package com.brogent.urls;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.net.Uri;
import android.os.Bundle;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.text.style.URLSpan;
import android.util.Log;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.TextView;

public class GetUrl extends Activity {
    private final String TAG = "GetUrl";
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView mesg = (TextView) findViewById(R.id.mesg);
        mesg.setLinksClickable(true);
        mesg.setMovementMethod(LinkMovementMethod.getInstance());
        mesg.setText(
                Html.fromHtml(
                    "Text with a " +
                    "<a href=\"http://www.google.com\">google</a> " +
                    "created in the Java source code using HTML." +
                    "<a href=\"http://tw.yahoo.com\">yahoo</a> "));
        mesg.setOnLongClickListener(new OnLongClickListener(){

            public boolean onLongClick(View v) {
                final URLSpan[] urls = ((TextView) v).getUrls();
                Log.e(TAG, "urls.length = " + urls.length);
                String[] urlStrings = new String[urls.length];
                for (int i = 0; i < urls.length; i++) {
                    Log.e(TAG, urls[i].getURL());
                    urlStrings[i] = urls[i].getURL();
                }
                new AlertDialog.Builder(GetUrl.this).setTitle("aloha").setItems(urlStrings, new OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Intent viewIntent = new Intent("android.intent.action.VIEW", Uri.parse(urls[which].getURL()));
                        startActivity(viewIntent);
                    }
                }).setPositiveButton("OK", null).show();
                return false;
            }
        });
    }
}